//设置
import React, { Component } from 'react'
import {
    View,
    Text,
    Image,
    TouchableOpacity,
} from 'react-native';

import { resumeoneStyle as styles } from '../../../../../styles';
import Icon from 'react-native-vector-icons/Ionicons';
import fmt from '../../../../util/fmtDateUtil';
import Modal from 'react-native-modal';
import { common_url, upLoading ,fetchRequest } from '../../../../util/fetch';

//图片选择器
import ImagePicker from 'react-native-image-crop-picker';

var Dimensions = require('Dimensions');
var width = Dimensions.get('window').width;
var height = Dimensions.get('window').height;


export default class Resumetop extends Component {
    // 初始化模拟数据
    constructor(props) {            //构造器只执行一次
        super(props);
        this.state = {
            isModal: false
        }
    };

    state = {
        isModalVisible: false,
    }
    _showModal = () => this.setState({ isModalVisible: true })
    _hideModal = () => this.setState({ isModalVisible: false })


    render() {
        return (
            <View style={[styles.resumeone, { width: width * 1 }]}>
                <View style={styles.icontop}>
                    <TouchableOpacity onPress={() => {
                        this.props.navigation.goBack()
                    }}
                        activeOpacity={1} style={styles.touch}>
                        <Icon
                            name={'ios-arrow-back'}
                            size={25}
                            color={'#ffffff'} />
                    </TouchableOpacity>
                    <TouchableOpacity onPress={this._showModal}>
                            <Image
                                source={this.props.resume.resumeIntentField ? { uri: common_url + "uploadImg/" + this.props.resume.resumeIntentField } : require('../../../../img/app/boy2.png')}
                                style={[styles.head]} />
                        </TouchableOpacity>
                    <TouchableOpacity activeOpacity={1}
                        onPress={() => {
                            this.goGet('Rtopedit', this.props.resume, this.props.actions)
                        }} style={styles.touch}>
                        <Icon
                            name={'ios-create'}
                            size={25}
                            color={'#ffffff'}
                        />
                    </TouchableOpacity>
                </View>

                <View style={styles.rescen}>
            
                    <View style={styles.onetx}>
                        <View style={styles.one1}>
                            <Text
                                style={[styles.one1t, { width: width * 0.3 }]}>姓名：<Text>{this.props.resume.resumeName}</Text></Text>
                            <Text
                                style={[styles.one1t, { width: width * 0.35 }]}>出生日期：<Text>{fmt(this.props.resume.resumeBirth, 'yyyy-MM-dd')}</Text></Text>
                            <Text
                                style={[styles.one1t, { width: width * 0.2 }]}>性别：<Text>{this.props.resume.resumeSex == 1 ? '男' : this.props.resume.resumeSex == 2 ? '女' : null}</Text></Text>
                        </View>
                        <Text style={styles.one2}>当前职位：<Text>{this.props.resume.resumePosition}</Text></Text>
                        <Text style={styles.one3}>当前城市：<Text>{this.props.resume.resumeWorkspace}</Text></Text>
                        <Text style={styles.one3}>当前行业：<Text>{this.fieldName(this.props.fields, 9)}</Text></Text>
                    </View>
                </View>

                <Modal isVisible={this.state.isModalVisible}
                    animationIn={'slideInLeft'}
                    animationOut={'slideOutRight'}>
                    <View style={styles.modal_in}>
                        <View style={styles.modal_touch}>
                            <TouchableOpacity onPress={this.xiangc.bind(this)} style={styles.modal_but}>
                                <Text style={styles.modal_txt1}>图库</Text>
                            </TouchableOpacity>
                            <TouchableOpacity onPress={this.pais.bind(this)} style={styles.modal_but}>
                                <Text style={styles.modal_txt1}>拍摄</Text>
                            </TouchableOpacity>
                        </View>
                        <TouchableOpacity style={styles.modal_butb}>
                            <Text onPress={this._hideModal} style={styles.modal_yes}>取消</Text>
                        </TouchableOpacity>
                    </View>
                </Modal>
            </View>
        )
    }

    //相册选择
    xiangc() {
        ImagePicker.openPicker({
            width: 300,
            height: 400,
            cropping: true,
            cropperCircleOverlay: true
        }).then(image => {
            this.setState({
                avatarSource: { uri: image.path }
            });
            this._upload(image);
            this._hideModal()
        }).catch(e => {
            this._hideModal()
        });
    }

    //拍摄选择
    pais() {
        ImagePicker.openCamera({
            width: 300,
            height: 400,
            cropping: true,
            cropperCircleOverlay: true
        }).then(image => {
            this.setState({
                avatarSource: { uri: image.path }
            });
            this._upload(image);
            this._hideModal()
        }).catch(e => {
            this._hideModal()
        });
    }
    
    _upload(image) {
        upLoading("upload/img.go", image).then(
            e => {
                if(e.msg=="ok"){
                    fetchRequest("Resume/uploadRusumeImg.go","POST",{resumeId:this.props.resume.resumeId,imgName:e.imgName}).then(e=>{
                        this.props.actions.EditResumeAction();
                    });
                }
            }
        )
    }

    //经营领域
    fieldName(List, sl) {
        var data = {
            cont: List
        }
        var arr = '';                            //用来装公司类型
        var s = sl;
        var length = data.cont.length;
        if (length <= s) {
            s = length;
        }
        for (var i = 0; i < s; i++) {
            if (data.cont[i].fieldType == '2') {
                arr += data.cont[i].fieldName + '/';
            }
        }
        if (arr.endsWith('/')) {
            arr = arr.substr(0, arr.length - 1);
        }
        return arr;
    }

    goGet(name, object, action) {
        this.props.navigation.navigate(name, { resume: object, actions: action });
    }

}